package com.metadata_system.api_server.auth_server;


import org.springframework.web.bind.annotation.*;
import com.metadata_system.api.annotations.ApiCheckSign;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.dto.LoginForm;
import com.metadata_system.common.http.request_dto.UserLoginRequest;
import com.metadata_system.common.service.ImageVerifyCodeService;
import com.metadata_system.common.service.SliderCaptchaService;
import com.metadata_system.common.service.UserService;
import com.metadata_system.common.utils.AddressUtil;
import com.metadata_system.common.utils.AesUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

@RestController("authPublicController")
@RequestMapping(value = "/api/auth-server/v001/Public/")
@Slf4j
public class PublicController {
    @Autowired
    UserService userService;
    @Autowired
    ImageVerifyCodeService imageVerifyCodeService;
    @Autowired
    SliderCaptchaService sliderCaptchaService;

    @RequestMapping(value = "r7wqyztzenb5k2k1", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiCheckSign
    public ApiResult r7wqyztzenb5k2k1(
            HttpServletRequest request,
            @RequestParam String data
    ) {
        LoginForm loginForm = null;
        try {
            String jsonString = AesUtils.Decrypt(data);
            loginForm = JSON.parseObject(jsonString, LoginForm.class);
        } catch (Exception e) {
            return ApiResult.error("数据异常");
        }
//        ApiResult checkResult = this.sliderCaptchaService.check(DeviceId, "Login", loginForm.getSliderX());
//        if (checkResult.getCode() > 0) {
//            return checkResult;
//        }

        UserLoginRequest userLoginRequest = UserLoginRequest.builder()
                .platform(StringUtils.isBlank(request.getHeader("Platform")) ? "PC" : request.getHeader("Platform"))
                .username(loginForm.getUsername())
                .password(loginForm.getPassword())
                .deviceId(request.getHeader("DeviceId"))
                .ip(AddressUtil.GetAddressIp())
                .build();
        return this.userService.login(userLoginRequest);
    }


    @RequestMapping("slider_captcha_generate")
    @ApiCheckSign
    public ApiResult slider_captcha_option(
            @RequestHeader String DeviceId,
            @RequestParam(required = false, defaultValue = "SendEmail") String eventType
    ) {
        return this.sliderCaptchaService.generate(DeviceId, eventType);
    }


    @RequestMapping("slider_captcha_check")
    @ApiCheckSign
    public ApiResult slider_captcha_check(
            @RequestHeader String DeviceId,
            @RequestParam(required = false, defaultValue = "SendEmail") String eventType,
            @RequestParam Integer sliderX
    ) {
        return this.sliderCaptchaService.check(DeviceId, eventType, sliderX);
    }

}
